package com.ftg.learn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ftg.learn.entity.User;
import com.ftg.learn.mapper.UserMapper;
import com.ftg.learn.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huye
 * @since 2022-03-11
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Override
    public Map<String,Object> checkUser(String account, String password) {
        Map<String,Object> map = new HashMap<>();
        //    public String checkUser(String account, String password, HttpSession session) {
        if(null == account||"".equalsIgnoreCase(account)){
            map.put("mess","用户名不能为空");
            return map;
        }else if(null == password||"".equalsIgnoreCase(password)){
            map.put("mess","密码不能为空");
            return map;
        }else{
            byte[] bs1 = new byte[0];
            try {
                bs1 = MessageDigest.getInstance("md5").digest(password.getBytes());
                password = new BigInteger(1, bs1).toString(16);
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
            //password 就是加密以后的密文
            User user = super.baseMapper.checkUser(account,password);
            if(Objects.isNull(user)){
                map.put("mess","当前用户不存在!!!");
                return map;
            }else{
                //                //我想把当前用户存储起来  session  客户端与服务端 私有缓存
                //                session.setAttribute("userName",user.getUname());
                //                session.setAttribute("userId",user.getUid());
                map.put("mess","登陆成功!!!");
                map.put("userName",user.getUserName());
                return map;
            }
        }
    }

    @Override
    public boolean saveForPassword(User e) {
        byte[] bs1 = new byte[0];
        try {
            bs1 = MessageDigest.getInstance("md5").digest(e.getUserPassword().getBytes());
            e.setUserPassword(new BigInteger(1, bs1).toString(16));
        } catch (NoSuchAlgorithmException e1) {
            e1.printStackTrace();
        }
        QueryWrapper<User> query = new QueryWrapper<>();
        query.eq("user_account",e.getUserAccount());
        User user = this.baseMapper.selectOne(query);
        if(Objects.isNull(user)){
            return this.baseMapper.insert(e) >= 0 ? true : false;
        }
        return false;
    }

    @Override
    public boolean updateIce(int id,int num) {
        int sum = super.baseMapper.updateForUstatus(id,num);
        return  sum >= 0 ? true : false;
    }

    @Override
    public boolean updateUserInfo(Long uid, String uaccount, String uname) {
        return super.baseMapper.updateUserInfo(uid,uaccount,uname);
    }

    @Override
    public boolean resetPassword(Long uid) {
        String newPass = "000000";
        byte[] bs1;
        try {
            bs1 = MessageDigest.getInstance("md5").digest(newPass.getBytes());
            newPass = new BigInteger(1, bs1).toString(16);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return super.baseMapper.resetPassword(uid,newPass);
    }

}


